﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WFNetLib.WinAPI;
using WFNetLib;
using System.Runtime.InteropServices;

namespace RobotControl
{
    public partial class Form_Joy : Form
    {
        public Form_Joy()
        {
            InitializeComponent();
        }
        推进器 tjq;
        bool[] bPress;
        private void Form_Joy_Load(object sender, EventArgs e)
        {
            bPress = new bool[15];
            for(int i=0;i<15;i++)
                bPress[i] = false;
            int result = 0;
            JOYCAPS caps = new JOYCAPS();
            if (Win32API.joyGetNumDevs() != 0)
            {
                //拥有手柄.则判断手柄状态
                result = Win32API.joyGetDevCaps(Joy.JOYSTICKID1, ref caps, Marshal.SizeOf(typeof(JOYCAPS)));
                if (result == Joy.JOYERR_NOERROR)
                {
                    this.Text = "手柄" + caps.szPname;
                    timer1.Interval = caps.wPeriodMin * 2;
                    timer1.Start();
                    tjq = new 推进器();
                }
                else
                {
                    MessageBox.Show("无法获得手柄信息");
                    this.Close();
                }
            }
            else
            {
                MessageBox.Show("没有找到手柄");
                this.Close();
            }
            trackBar1.Value = 推进器.Power;
            label3.Text = trackBar1.Value.ToString();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            JOYINFOEX infoEx = new JOYINFOEX();
            infoEx.dwSize = Marshal.SizeOf(typeof(JOYINFOEX));
            infoEx.dwFlags = (int)JOYINFOEX_dwFlags.JOY_RETURNBUTTONS;

            int result = Win32API.joyGetPosEx(Joy.JOYSTICKID1, ref infoEx);
            if (result == Joy.JOYERR_NOERROR)
            {
                if (BytesOP.GetBit(infoEx.dwButtons, 0))//1,左旋
                {
                    label2.BackColor = Color.Red;  
                    if(bPress[11])//之前已经在左右平移
                    {
                        label2.BackColor=SystemColors.Control;
                    }
                    else
                    {
                        bPress[1]=true;
                        //左旋
                        tjq.GoLeftRound();
                    }
                }
                else
                {
                    label2.BackColor=SystemColors.Control;
                    if(bPress[1])
                    {
                        bPress[1]=false;
                        //停止左旋
                        tjq.Stop(1);
                    }
                }
                if (BytesOP.GetBit(infoEx.dwButtons, 1))//2,下潜
                {
                    label12.BackColor = Color.Red;
                    bPress[2]=true;
                    //下潜
                }
                else
                {
                    label12.BackColor = SystemColors.Control;
                    if (bPress[2])
                    {
                        bPress[2] = false;
                        //停止下潜
                    }
                }
                if (BytesOP.GetBit(infoEx.dwButtons, 2))//3,上浮
                {
                    label4.BackColor = Color.Red;
                    bPress[3] = true;
                    //上浮
                }
                else
                {
                    label4.BackColor = SystemColors.Control;
                    if (bPress[2])
                    {
                        bPress[2] = false;
                        //停止下潜
                    }
                }
                if (BytesOP.GetBit(infoEx.dwButtons, 3))//4,右旋
                {
                    label5.BackColor = Color.Red;
                    if(bPress[11])//之前已经在左右平移
                    {
                        label5.BackColor=SystemColors.Control;
                    }
                    else
                    {
                        bPress[4]=true;
                        //右旋
                        tjq.GoRightRound();
                    }
                }
                else
                {
                    label5.BackColor = SystemColors.Control;
                    if (bPress[4])
                    {
                        bPress[4] = false;
                        //停止右旋
                        tjq.Stop(1);
                    }
                }
                if (BytesOP.GetBit(infoEx.dwButtons, 4))//5
                {
                    label8.BackColor = Color.Red;
                    tjq.Stop(trackBar2.Value + 10);
                }
                else
                {
                    label8.BackColor = SystemColors.Control;
                }
                if (BytesOP.GetBit(infoEx.dwButtons, 5))//6
                {
                    label11.BackColor = Color.Red;
                    tjq.Test(trackBar2.Value, checkBox1.Checked);
                }
                else
                {
                    label11.BackColor = SystemColors.Control;
                }
                if (BytesOP.GetBit(infoEx.dwButtons, 6))//7
                {
                    label9.BackColor = Color.Red;
                }
                else
                {
                    label9.BackColor = SystemColors.Control;
                }
                if (BytesOP.GetBit(infoEx.dwButtons, 7))//8
                {
                    label10.BackColor = Color.Red;
                }
                else
                {
                    label10.BackColor = SystemColors.Control;
                }
                if (BytesOP.GetBit(infoEx.dwButtons, 8))//9
                {
                    label6.BackColor = Color.Red;
                }
                else
                {
                    label6.BackColor = SystemColors.Control;
                }
                if (BytesOP.GetBit(infoEx.dwButtons, 9))//10,急停
                {
                    label7.BackColor = Color.Red;
                    tjq.Stop(-1);
                }
                else
                {
                    label7.BackColor = SystemColors.Control;
                }
                int m = 0xFFFF / 2;                             //中心点的值,偏差0x100
                int x, y;
                x = 0;
                y = 0;
                if ((infoEx.dwXpos - m) > 0x100)
                {
                    x++;
                }
                else if ((m - infoEx.dwXpos) > 0x100)
                {
                    x--;
                }
                if ((infoEx.dwYpos - m) > 0x100)
                {
                    y++;
                }
                else if ((m - infoEx.dwYpos) > 0x100)
                {
                    y--;
                }
                if (x == 0)
                {
                    if (bPress[11])
                    {
                        bPress[11] = false;
                        //停止左右旋
                        tjq.Stop(1);
                    }
                    if (y == 0)
                    {
                        this.label1.Text = "";  
                    }
                    else if (y == 1)
                    {
                        this.label1.Text = "后退";
                        this.label1.TextAlign = ContentAlignment.BottomCenter;
                        tjq.GoBack();
                    }
                    else if (y == -1)
                    {
                        this.label1.Text = "前进";
                        this.label1.TextAlign = ContentAlignment.TopCenter;
                        tjq.GoHead();
                        Form_Main.Heading = Form_Main.sdHX;
                        Form_Main.定向 = true;
                    }
                }
                else
                {
                    if (!bPress[1] && !bPress[4])
                    {
                        bPress[11] = true;
                    }
                }
                if (y == 0)
                {
                    if (bPress[12])
                    {
                        bPress[12] = false;
                        //停止前进或后退
                        tjq.Stop(2);
                        Form_Main.StopHeading();
                    }                    
                }
                else
                {
                    bPress[12] = true;
                }
                if (x == 0 && y == 0) //无
                {
                    this.label1.Text="";                    
                }
                if (x == 1)
                {
                    if (bPress[11])
                    {
                        if (y == 0)
                        {
                            this.label1.Text = "右移";
                            this.label1.TextAlign = ContentAlignment.MiddleRight;
                            tjq.GoRight();
                        }
                        else if (y == 1)
                        {
                            this.label1.Text = "右后";
                            this.label1.TextAlign = ContentAlignment.BottomRight;
                            tjq.GoRight();
                            tjq.GoBack();
                        }
                        else if (y == -1)
                        {
                            this.label1.Text = "右上";
                            this.label1.TextAlign = ContentAlignment.TopRight;
                            tjq.GoRight();
                            tjq.GoHead();
                        }
                    }
                }
                else if (x == -1)
                {
                    if (bPress[11])
                    {
                        if (y == 0)
                        {
                            this.label1.Text = "左移";
                            this.label1.TextAlign = ContentAlignment.MiddleLeft;
                            tjq.GoLeft();
                        }
                        else if (y == 1)
                        {
                            this.label1.Text = "左后";
                            this.label1.TextAlign = ContentAlignment.BottomLeft;
                            tjq.GoLeft();
                            tjq.GoBack();
                        }
                        else if (y == -1)
                        {
                            this.label1.Text = "左上";
                            this.label1.TextAlign = ContentAlignment.TopLeft;
                            tjq.GoLeft();
                            tjq.GoHead();
                        }
                    }
                }         
            }
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            推进器.Power = trackBar1.Value;
            label3.Text = trackBar1.Value.ToString();
        }
    }
}
